package controllers

import (
	"cute0517/cache"
	"cute0517/models"
	"encoding/json"
	"fmt"
	beego "github.com/beego/beego/v2/server/web"
	"io"
	"os"
	"path/filepath"
	"time"
)

type CuteController struct {
	beego.Controller
}

type PageCount struct {
	Data  interface{} `json:"data"`
	Count int64
}

func (c *CuteController) Add() {
	image := c.GetString("image")
	name := c.GetString("name")
	shu := c.GetString("shu")
	uptime := c.GetString("uptime")
	createTime := c.GetString("createTime")
	typesId, _ := c.GetInt("typesId")

	if image == "" || name == "" || shu == "" || typesId == 0 {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "内容不能为空",
		}
		c.ServeJSON()
		return
	}

	cute := models.Cute{
		Image:      image,
		Name:       name,
		Shu:        shu,
		UpTime:     uptime,
		CreateTime: createTime,
		TypesId:    typesId,
	}

	if err := models.DB.Create(&cute).Error; err != nil {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "添加失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code:    200,
		Data:    cute,
		Message: "添加成功",
	}
	c.ServeJSON()
	return
}

func (c *CuteController) Upload() {
	file, m, err := c.GetFile("image")
	if err != nil {
		return
	}

	if m.Size > 2048*2048*1 {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "图片大小不正确",
		}
		c.ServeJSON()
		return
	}

	ext := filepath.Ext(m.Filename)
	if ext != ".png" && ext != ".jpg" {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "图片格式不正确",
		}
		c.ServeJSON()
		return
	}

	openFile, err := os.OpenFile("static/img/"+m.Filename, os.O_CREATE|os.O_RDWR|os.O_APPEND, 0777)
	if err != nil {
		return
	}

	io.Copy(openFile, file)
	c.Data["json"] = Res{
		Code:    200,
		Data:    "http://127.0.0.1:8080/static/img/" + m.Filename,
		Message: "图片上传成功",
	}
	c.ServeJSON()
	return
}

func (c *CuteController) List() {

	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")

	var cute []models.Cute
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 3)
	i := (page - 1) * size

	key := "cute_list_"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData == "" {
		if err := models.DB.Offset(i).Limit(size).Find(&cute).Error; err != nil {
			c.Data["json"] = Res{
				Code:    500,
				Data:    nil,
				Message: "查询失败",
			}
			c.ServeJSON()
			return
		}
		jsonData, _ := json.Marshal(cute)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
	} else {
		fmt.Println("获取成功")
		json.Unmarshal([]byte(cacheData), &cute)
	}

	var count int64
	models.DB.Model(&cute).Count(&count)

	c.Data["json"] = Res{
		Code: 200,
		Data: PageCount{
			Data:  cute,
			Count: count,
		},
		Message: "查询成功",
	}
	c.ServeJSON()
	return
}

func (c *CuteController) Del() {
	id, _ := c.GetInt("id")

	if err := models.DB.Delete(&models.Cute{}, id).Error; err != nil {
		c.Data["json"] = Res{
			Code:    500,
			Data:    nil,
			Message: "删除失败",
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = Res{
		Code:    200,
		Data:    nil,
		Message: "删除成功",
	}
	c.ServeJSON()
	return
}

func (c *CuteController) Detail() {
	id, _ := c.GetInt("id")

	var cute models.Cute
	key := "cute_list_"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, key).Result()
	if cacheData != "" {
		if err := models.DB.Where("id = ?", id).First(&cute).Error; err != nil {
			c.Data["json"] = Res{
				Code:    500,
				Data:    nil,
				Message: "详情查询失败",
			}
			c.ServeJSON()
			return
		}
		jsonData, _ := json.Marshal(cute)
		cache.Rdb.Set(cache.Ctx, key, jsonData, time.Hour*24)
	} else {
		fmt.Println("获取成功")
		json.Unmarshal([]byte(cacheData), &cute)
	}
	c.Data["json"] = Res{
		Code:    200,
		Data:    cute,
		Message: "详情查询成功",
	}
	c.ServeJSON()
	return

}
